<script type="text/javascript">
    $(document).ready(function(){
        $("a#fancy_box").fancybox();
        $("#pages_table").dataTable();
        $("#characters_table").dataTable();
    });
    
</script>

<h2><?php echo $manga_title; ?></h2>

<div id="pages_list">
    <h3>All pages</h3>

    <?php
    if ($all_pages->num_rows() > 0) {

        $tmpl = array(
            'table_open' => '<table id="pages_table" cellpadding="0" cellspacing="0" border="0" class="display">',
            'row_start'           => '<tr class="gradeA">',
            
            );
        $this->table->set_template($tmpl);

        $this->table->set_heading('Page', 'Name', 'Image', 'Delete');

        foreach ($all_pages->result() as $page) {
            $page_num = $page->page_num;
            $name = $page->name;
            $image = anchor(base_url() . $page->full_path, 'View', 'id="fancy_box" title="Page: ' . $page_num . '"');
            $delete = anchor($controller_url . 'delete_page/' . $page->image_id, 'Delete');

            $this->table->add_row($page_num, $name, $image, $delete);
        }

        echo $this->table->generate();
    } else {
        echo "<p>The manga contains no page. Please upload.</p>";
    }
    ?>

</div>

<div id="characters_list">
    <h3>All Characters</h3>

    <?php
    if ($all_characters->num_rows() > 0) {

        $tmpl = array(
            'table_open' => '<table id="characters_table" cellpadding="0" cellspacing="0" border="0" class="display">',
            'row_start'           => '<tr class="gradeA">',
            );
        $this->table->set_template($tmpl);

        $this->table->set_heading('Name', 'Personality', 'View', 'Delete');

        foreach ($all_characters->result() as $character) {
            $id = $character->character_id;
            $name = $character->name;
            $personality = $character->personality_type;
            $image = anchor(base_url() . $character->full_path, 'View', 'id="fancy_box" title="' . $name . '"');
            $delete = anchor($controller_url . 'delete_char/' . $id, 'Delete');

            $this->table->add_row($name, $personality, $image, $delete);
        }

        echo $this->table->generate();
    } else {
        echo "<p>The manga has no characters. Please upload</p>";
    }
    ?>
</div>

<div id="upload_box">

    <h3>Upload Page</h3>

    <?php
    $attributes = array(
        'class' => 'upload_box',
        'id' => 'upload_img');

    echo form_open_multipart($controller_url . 'upload_page', $attributes);

    echo form_label('Page', 'page');

    $upload_options = array();

    if ($all_pages->num_rows() > 0) {

        //Get current available pages    
        $current_pages_arr = array();
        foreach ($all_pages->result() as $page) {
            //Store page numbers in an array for uploading purpose
            array_push($current_pages_arr, $page->page_num);
        }

        //Get array of all pages in correct order
        $all_pages_num = range(0, $last_page + 1);

        //Comparision to find out pages need to be uploaded
        $pages_to_upload = array_diff($all_pages_num, $current_pages_arr);

        foreach ($pages_to_upload as $page) {
            if ($page === 0) {
                $upload_options[$page] = 'Cover';
            } else {
                $upload_options[$page] = $page;
            }
        }
    } else {
        $upload_options[0] = 'Cover';
    }

    echo form_dropdown('pages', $upload_options);

    echo form_label('Source', 'source');

    echo form_upload('upload_page');

    echo form_submit('submit', 'Upload');

    echo form_close();
    ?>

    <h3>Upload Character</h3>

    <?php
//Display upload form for characters that not yet uploaded
//Step 1: Create array of all personality types
    $all_chars = array('realistic', 'investigate', 'artistic', 'social', 'enterprising', 'conventional');

//Step 2: Comparsion between all personality types and current personality types - return array of characters that have not been uploaded
    $upload_chars = array_diff($all_chars, $avail_personality); //$avail_personality sent from controller

    if (count($upload_chars) === 0) {
        echo "You uploaded all characters";
    } else {

        $attributes = array(
            'class' => 'upload_box',
            'id' => 'upload_char');

        echo form_open_multipart($controller_url . 'upload_char', $attributes);

        echo form_label('Character', 'char');

        $upload_options = array();

        foreach ($upload_chars as $char) {
            $upload_options[$char] = ucfirst($char);
        }

        echo form_dropdown('personality', $upload_options);

        echo form_label('Source', 'source');

        echo form_upload('upload_char');

        echo form_submit('submit', 'Upload');

        echo form_close();
    }
    ?>

    <div id="uploading_errors">
        <?php echo $this->session->userdata('uploading_errors'); ?>
    </div>

</div>

<div id="btn_back">
    <?php echo anchor($controller_url, 'Back to list of manga'); ?>
</div>

